package com.ledim.db.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.ledim.bean.LedimPicBean;

import com.ledim.bean.LedimUserBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "USER".
*/
public class LedimUserBeanDao extends AbstractDao<LedimUserBean, Long> {

    public static final String TABLENAME = "USER";

    /**
     * Properties of entity LedimUserBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "Id");
        public final static Property UserId = new Property(1, String.class, "userId", false, "user_id");
        public final static Property Username = new Property(2, String.class, "username", false, "username");
        public final static Property Nickname = new Property(3, String.class, "nickname", false, "nickname");
        public final static Property Email = new Property(4, String.class, "email", false, "email");
        public final static Property Mobile = new Property(5, String.class, "mobile", false, "mobile");
        public final static Property Gender = new Property(6, String.class, "gender", false, "gender");
        public final static Property AvatarId = new Property(7, long.class, "avatarId", false, "avatar");
        public final static Property Coin = new Property(8, String.class, "coin", false, "coin");
        public final static Property Status = new Property(9, String.class, "status", false, "status");
        public final static Property Max_coin = new Property(10, String.class, "max_coin", false, "max_coin");
        public final static Property Max_power = new Property(11, String.class, "max_power", false, "max_power");
        public final static Property Power = new Property(12, String.class, "power", false, "power");
        public final static Property Is_signed = new Property(13, boolean.class, "is_signed", false, "is_signed");
    };

    private DaoSession daoSession;


    public LedimUserBeanDao(DaoConfig config) {
        super(config);
    }
    
    public LedimUserBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"USER\" (" + //
                "\"Id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"user_id\" TEXT UNIQUE ," + // 1: userId
                "\"username\" TEXT," + // 2: username
                "\"nickname\" TEXT," + // 3: nickname
                "\"email\" TEXT," + // 4: email
                "\"mobile\" TEXT," + // 5: mobile
                "\"gender\" TEXT," + // 6: gender
                "\"avatar\" INTEGER NOT NULL ," + // 7: avatarId
                "\"coin\" TEXT," + // 8: coin
                "\"status\" TEXT," + // 9: status
                "\"max_coin\" TEXT," + // 10: max_coin
                "\"max_power\" TEXT," + // 11: max_power
                "\"power\" TEXT," + // 12: power
                "\"is_signed\" INTEGER NOT NULL );"); // 13: is_signed
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"USER\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, LedimUserBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String userId = entity.getUserId();
        if (userId != null) {
            stmt.bindString(2, userId);
        }
 
        String username = entity.getUsername();
        if (username != null) {
            stmt.bindString(3, username);
        }
 
        String nickname = entity.getNickname();
        if (nickname != null) {
            stmt.bindString(4, nickname);
        }
 
        String email = entity.getEmail();
        if (email != null) {
            stmt.bindString(5, email);
        }
 
        String mobile = entity.getMobile();
        if (mobile != null) {
            stmt.bindString(6, mobile);
        }
 
        String gender = entity.getGender();
        if (gender != null) {
            stmt.bindString(7, gender);
        }
        stmt.bindLong(8, entity.getAvatarId());
 
        String coin = entity.getCoin();
        if (coin != null) {
            stmt.bindString(9, coin);
        }
 
        String status = entity.getStatus();
        if (status != null) {
            stmt.bindString(10, status);
        }
 
        String max_coin = entity.getMax_coin();
        if (max_coin != null) {
            stmt.bindString(11, max_coin);
        }
 
        String max_power = entity.getMax_power();
        if (max_power != null) {
            stmt.bindString(12, max_power);
        }
 
        String power = entity.getPower();
        if (power != null) {
            stmt.bindString(13, power);
        }
        stmt.bindLong(14, entity.getIs_signed() ? 1L: 0L);
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, LedimUserBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String userId = entity.getUserId();
        if (userId != null) {
            stmt.bindString(2, userId);
        }
 
        String username = entity.getUsername();
        if (username != null) {
            stmt.bindString(3, username);
        }
 
        String nickname = entity.getNickname();
        if (nickname != null) {
            stmt.bindString(4, nickname);
        }
 
        String email = entity.getEmail();
        if (email != null) {
            stmt.bindString(5, email);
        }
 
        String mobile = entity.getMobile();
        if (mobile != null) {
            stmt.bindString(6, mobile);
        }
 
        String gender = entity.getGender();
        if (gender != null) {
            stmt.bindString(7, gender);
        }
        stmt.bindLong(8, entity.getAvatarId());
 
        String coin = entity.getCoin();
        if (coin != null) {
            stmt.bindString(9, coin);
        }
 
        String status = entity.getStatus();
        if (status != null) {
            stmt.bindString(10, status);
        }
 
        String max_coin = entity.getMax_coin();
        if (max_coin != null) {
            stmt.bindString(11, max_coin);
        }
 
        String max_power = entity.getMax_power();
        if (max_power != null) {
            stmt.bindString(12, max_power);
        }
 
        String power = entity.getPower();
        if (power != null) {
            stmt.bindString(13, power);
        }
        stmt.bindLong(14, entity.getIs_signed() ? 1L: 0L);
    }

    @Override
    protected final void attachEntity(LedimUserBean entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public LedimUserBean readEntity(Cursor cursor, int offset) {
        LedimUserBean entity = new LedimUserBean( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // userId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // username
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // nickname
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // email
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // mobile
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // gender
            cursor.getLong(offset + 7), // avatarId
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // coin
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // status
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10), // max_coin
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // max_power
            cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12), // power
            cursor.getShort(offset + 13) != 0 // is_signed
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, LedimUserBean entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setUserId(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setUsername(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setNickname(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setEmail(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setMobile(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setGender(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setAvatarId(cursor.getLong(offset + 7));
        entity.setCoin(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setStatus(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setMax_coin(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
        entity.setMax_power(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.setPower(cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12));
        entity.setIs_signed(cursor.getShort(offset + 13) != 0);
     }
    
    @Override
    protected final Long updateKeyAfterInsert(LedimUserBean entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(LedimUserBean entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getLedimPicBeanDao().getAllColumns());
            builder.append(" FROM USER T");
            builder.append(" LEFT JOIN PHOTO T0 ON T.\"avatar\"=T0.\"Id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected LedimUserBean loadCurrentDeep(Cursor cursor, boolean lock) {
        LedimUserBean entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        LedimPicBean avatar = loadCurrentOther(daoSession.getLedimPicBeanDao(), cursor, offset);
         if(avatar != null) {
            entity.setAvatar(avatar);
        }

        return entity;    
    }

    public LedimUserBean loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<LedimUserBean> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<LedimUserBean> list = new ArrayList<LedimUserBean>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<LedimUserBean> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<LedimUserBean> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
